//
//  WDBaseSearchView.swift
//  UIKitExtentions
//
//  Created by Alean on 2023/4/4.
//

import Foundation

extension WDBaseSearchView: UITextFieldDelegate {
    open func textFieldDidEndEditing(_ textField: UITextField) {
        print(textField.text)
    }
    open func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return shouldBeginEdited?(textField) ?? true
    }
}

open class WDBaseSearchView: UIView {
    open lazy var backgroundView = {
        let view = UIView()
        view.layer.borderColor = UIColor.hexString("#ED556A").cgColor
        view.layer.borderWidth = 2.0
        return view
    }()
    
    open lazy var textField: UITextField = {
        let text = UITextField()
        text.wd.placeholder("输入店铺名称试试吧")
        text.clearButtonMode = .whileEditing
        text.tintColor = UIColor.hexString("#ED556A")
        text.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        text.textColor = UIColor.hexString("#333333")
        text.delegate = self
        return text
    }()

    open lazy var searchBtn: UIButton = {
        let btn = UIButton(normalTitle: "搜索",
                           normalColor: UIColor.hexString("#FFFFFF"),
                           target: self,
                           action: #selector(searchBtnClicked(_:)),
                           event: .touchUpInside)
        btn.backgroundColor = UIColor.hexString("#ED556A")
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        return btn
    }()

    open var placeholder: String = "" {
        didSet {
            textField.placeholder = placeholder
        }
    }

    open var confirmCallback: ((Bool, Any?) -> Void)? = nil
    open var cancelCallback: (() -> Void)? = nil
    open var shouldBeginEdited: ((_ textField: UITextField) -> Bool)? = nil

    open var text: String = "" {
        didSet {
            textField.text = text
        }
    }

    deinit {
        
    }

//    @objc public init(confirmTitle: String,
//                      policy: LAPolicy = .deviceOwnerAuthentication,
//                      confirmCallback: @escaping (Bool, Any?) -> Void,
//                      cancelCallback: (() -> Void)? = nil) {
    public convenience init(confirmCallback: @escaping (Bool, Any?) -> Void,
                            cancelCallback: (() -> Void)? = nil) {
        self.init(frame: .zero)
        self.confirmCallback = confirmCallback
        self.cancelCallback = cancelCallback
    }

    public override init(frame: CGRect) {
        super.init(frame: frame)
        isUserInteractionEnabled = true
        backgroundColor = .clear
        initContentView()
    }

    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    open func initContentView() {
        addSubview(backgroundView)
        backgroundView.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.bottom.equalToSuperview()
        }
    }

    open override func layoutSubviews() {
        super.layoutSubviews()
        backgroundView.layer.cornerRadius = self.frame.size.height / 2.0
        searchBtn.layer.cornerRadius = searchBtn.frame.size.height / 2.0
    }

    open func didEndEditing() {
        textField.endEditing(true)
    }

    @objc func searchBtnClicked(_ sender: UIButton) {
        confirmCallback?(true, textField.text)
    }
}
